﻿Imports VN.INS.BL.BC
Imports System.Data

Partial Class Devida_PopupAFinancieroSemanal
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            ViewState("Nombre") = Request.QueryString("Nombre").ToString
            ViewState("POA") = Request.QueryString("POA").ToString
            ViewState("PPER") = Request.QueryString("PPER").ToString
            ViewState("PPER_ID") = Request.QueryString("PPER_ID").ToString
            ViewState("Ejecutor") = Request.QueryString("Ejecutor").ToString
            ViewState("Eje_Des") = Request.QueryString("Eje_Des").ToString
            ViewState("Nuevo") = Request.QueryString("Nuevo").ToString
            ViewState("Persona") = Request.QueryString("Persona").ToString
            lblPPER.Text = ViewState("PPER")
            lblActividad.Text = ViewState("Nombre")
            lblEjecutor.Text = ViewState("Eje_Des")
            CargarSemaforo()
            ddlSemaforo.SelectedValue = "1"
            txtColor.BackColor = Drawing.Color.Green

            Dim objGeneral As GeneralBC
            Dim strFecha As String = "#"
            Try
                objGeneral = New GeneralBC
                strFecha = objGeneral.FechaAvanceSemanal(ViewState("año"))
                If strFecha Is Nothing Then
                    ViewState("Periodo") = Request.QueryString("CodPeriodo").ToString
                    lblFecha.Text = Request.QueryString("Periodo").ToString
                Else
                    ' Dividimos la cadena en partes
                    Dim words As String() = strFecha.Split(New Char() {"#"c})
                    ' Recorremos todas las partes
                    Dim word As String
                    Dim i As Int16 = 0
                    For Each word In words
                        i = i + 1
                        If i = 1 Then
                            ViewState("Periodo") = word
                        End If
                        If i = 2 Then
                            lblFecha.Text = word
                            Exit For
                        End If
                    Next
                End If
            Catch ex As Exception
                Throw ex
            End Try
            Dim objReporte As ReporteBC
            Dim objDt As DataTable
            Try
                objReporte = New ReporteBC
                objDt = New DataTable
                objDt = objReporte.CargaAvanceFinancieroSemanal(ViewState("POA"), ViewState("Periodo"), ViewState("Periodo"))
                lblPresupuestado.Text = String.Format("{0:# ### ###,##}", objReporte.MontoPresupuestado(ViewState("POA")).ToString)
                If objDt.Rows.Count > 0 Then
                    ddlSemaforo.SelectedValue = objDt.Rows(0).Item("SEMAFORO")
                    ddlSemaforo_SelectedIndexChanged(sender, e)
                    txtProgramado.Text = objDt.Rows(0).Item("MONTO_PROGRAMADO_ANTERIOR").ToString
                    txtComprometido.Text = objDt.Rows(0).Item("MONTO_COMPROMETIDO").ToString
                    txtDevengado.Text = objDt.Rows(0).Item("MONTO_DEVENGADO").ToString
                    txtGirado.Text = objDt.Rows(0).Item("MONTO_GIRADO").ToString
                    txtObs.Text = objDt.Rows(0).Item("OBSERVACIONES").ToString
                    txtAcciones.Text = objDt.Rows(0).Item("ACCION").ToString
                End If
            Catch ex As Exception
                Throw ex
            End Try
        End If
    End Sub

    Public Sub CargarSemaforo()
        Dim objGeneral As GeneralBC
        Dim objDt As DataTable
        Try
            objGeneral = New GeneralBC
            objDt = New DataTable
            objDt = objGeneral.ListaTablaGeneral("SEMAFORO")
            ddlSemaforo.DataSource = objDt
            ddlSemaforo.DataTextField = "DESCRIPCION"
            ddlSemaforo.DataValueField = "CODIGO"
            ddlSemaforo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub ddlSemaforo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSemaforo.SelectedIndexChanged
        If ddlSemaforo.SelectedValue = "1" Then
            txtColor.BackColor = Drawing.Color.Green
        ElseIf ddlSemaforo.SelectedValue = "2" Then
            txtColor.BackColor = Drawing.Color.Orange
        ElseIf ddlSemaforo.SelectedValue = "3" Then
            txtColor.BackColor = Drawing.Color.Red
        ElseIf ddlSemaforo.SelectedValue = "4" Then
            txtColor.BackColor = Drawing.Color.White
        End If
    End Sub

    Protected Sub btn_guardar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_guardar.Click
        If txtObs.Text = "" Then
            alerta.Mostrar_Alerta("Aviso", "Debe ingresar la observación")
        ElseIf txtAcciones.Text = "" Then
            alerta.Mostrar_Alerta("Aviso", "Debe ingresar la acción")
        Else
            Dim objReporte As ReporteBC
            Try
                objReporte = New ReporteBC
                If ViewState("Nuevo") = "1" Then
                    objReporte.Insertar_Reporte_Semanal(ViewState("Periodo"), ViewState("POA"), ViewState("PPER_ID"), ViewState("Ejecutor"), txtAcciones.Text, "1", Convert.ToDouble(IIf(lblPresupuestado.Text = "", 0, lblPresupuestado.Text)), Convert.ToDouble(IIf(txtComprometido.Text = "", 0, txtComprometido.Text)), Convert.ToDouble(IIf(txtDevengado.Text = "", 0, txtDevengado.Text)), Convert.ToDouble(IIf(txtGirado.Text = "", 0, txtGirado.Text)), Convert.ToDouble(IIf(txtProgramado.Text = "", 0, txtProgramado.Text)), txtObs.Text, ddlSemaforo.SelectedValue.ToString, ViewState("Persona"))                    
                ElseIf ViewState("Nuevo") = "0" Then
                    objReporte.Actualizar_Reporte_Semanal(ViewState("Periodo"), ViewState("POA"), txtAcciones.Text, Convert.ToDouble(IIf(txtComprometido.Text = "", 0, txtComprometido.Text)), Convert.ToDouble(IIf(txtDevengado.Text = "", 0, txtDevengado.Text)), Convert.ToDouble(IIf(txtGirado.Text = "", 0, txtGirado.Text)), Convert.ToDouble(IIf(txtProgramado.Text = "", 0, txtProgramado.Text)), txtObs.Text, ddlSemaforo.SelectedValue.ToString, ViewState("Persona"))
                End If
                Response.Redirect("ListaAvances.aspx")
            Catch ex As Exception
                Throw ex
            End Try
        End If
    End Sub

    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        Response.Redirect("ListaAvances.aspx")
    End Sub


End Class
